-
Notifications
You must be signed in to change notification settings - Fork 0
[Week3] 전현수: 단풍잎 이야기, 킹, 크로스워드, 랜선 자르기 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
드디어 리뷰완료!
|
||
fun solution() { | ||
|
||
val (keyCnt, questCnt, skillPerQuest) = readln().split(" ").map { it.toInt() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skillPerQuest
변수 사용 안하고 있네요!
각각의 항목들에 어떤 용도인지 명시해주는 부분 좋은것 같아요 👍🏼
repeat(questCnt) { | ||
val quests = readln().split(" ").map { it.toInt() } | ||
questDataMap.add(quests) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add를 반복적으로 하는것 보다 애초에 초기화를 동시에 적용하게 repeat 대신에 Array 사용하고 바로 questDataMap 에 반영하는 구조는 어떠신가요?
questDataMap = Array(questCnt) {
readln().split(" ").map { it.toInt() }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러면 lateinit var questDataMap: Array<List<Int>>
로 선언하는게 효율적이겠죠?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리스트보다... Array에 입력과 동시에 초기화.. 좋은 것 같습니다!!
questDataMap.forEach { needKeyList -> | ||
if (needKeyList.all { it in pickedKey }) { | ||
curCanClearQuestCnt++ | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분 count 함수로 쓰면 ++ 연산 없이 될것같아요
val curCanClearQuestCnt = questDataMap.count { it.all { it in pickedKey } }
var canGetLanCnt = 0L | ||
// 조건 | ||
lanList.forEach { lan -> | ||
canGetLanCnt += lan / mid | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sumOf 사용해보시는건 어떤가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오... 그러네요 고차함수를 쓸 생각을 못했네요...!!!
val lanList = mutableListOf<Long>() | ||
|
||
repeat(haveCnt) { | ||
val lanInfo = readln().toLong() | ||
lanList.add(lanInfo) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정이 필요없는 타입인것 같은데 초기화하면서 넣어주는 방식은 어떨까요?
val lanList = Array(haveCnt) {
readln().toLong()
}
if ((king.x + nx in 0..7).not() || | ||
(king.y + ny in 0..7).not() | ||
) { | ||
return@repeat | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분도 contain 함수로 교체가능하죠?
if (!nKing.contain(0..7, 0..7)) {
return@repeat
}
val nx = dir.position.x | ||
val ny = dir.position.y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oprator 재정의해서!
val nKing = king + dir.position
if (king.x + nx == stone.x && | ||
king.y + ny == stone.y | ||
) { | ||
// 돌이 움직일 수 있는 위치라면 | ||
if (stone.x + nx in 0..7 && | ||
stone.y + ny in 0..7 | ||
) { | ||
// 돌 이동 후 킹 이동 | ||
stone.x += nx | ||
stone.y += ny | ||
king.x += nx | ||
king.y += ny | ||
} | ||
// 킹이 이동할 위치에 돌이 없다면 바로 이동 | ||
} else { | ||
king.x += nx | ||
king.y += ny | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분도 위랑 동일하게 공통화 할 수 있겠죠
if (nKing == stone) {
val nStone = stone + dir.position
// 돌이 움직일 수 있는 위치라면
if (nStone.contain(0..7, 0..7)) {
// 돌 이동 후 킹 이동
stone = nStone
king = nKing
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king = nKing
}
println(positionToCoordinate(king)) | ||
println(positionToCoordinate(stone)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
출력은 toString에 정의했으니 그냥 출력하면됩니다
println(king)
println(stone)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) 개행은 한번에 하면 더 좋겠죠?
println("$king\n$stone")
fun solution() { | ||
|
||
val (kingLocation, stoneLocation, moveCnt) = readln().split(" ") | ||
|
||
val king = GameCharacter() | ||
coordinateToPosition(kingLocation).apply { | ||
king.x = this.x | ||
king.y = this.y | ||
} | ||
|
||
val stone = GameCharacter() | ||
coordinateToPosition(stoneLocation).apply { | ||
stone.x = this.x | ||
stone.y = this.y | ||
} | ||
|
||
repeat(moveCnt.toInt()) { | ||
val command = readln() | ||
lateinit var dir: Dir | ||
when (command) { | ||
(Dir.R).toString() -> { | ||
dir = Dir.R | ||
} | ||
|
||
(Dir.L).toString() -> { | ||
dir = Dir.L | ||
} | ||
|
||
(Dir.B).toString() -> { | ||
dir = Dir.B | ||
} | ||
|
||
(Dir.T).toString() -> { | ||
dir = Dir.T | ||
} | ||
|
||
(Dir.RT).toString() -> { | ||
dir = Dir.RT | ||
} | ||
|
||
(Dir.LT).toString() -> { | ||
dir = Dir.LT | ||
} | ||
|
||
(Dir.RB).toString() -> { | ||
dir = Dir.RB | ||
} | ||
|
||
(Dir.LB).toString() -> { | ||
dir = Dir.LB | ||
} | ||
} | ||
|
||
val nx = dir.position.x | ||
val ny = dir.position.y | ||
|
||
// 킹이 이동할 수 없는 위치면 명령 무시 | ||
if ((king.x + nx in 0..7).not() || | ||
(king.y + ny in 0..7).not() | ||
) { | ||
return@repeat | ||
} | ||
// 킹이 이동할 위치에 돌이 있다면 | ||
if (king.x + nx == stone.x && | ||
king.y + ny == stone.y | ||
) { | ||
// 돌이 움직일 수 있는 위치라면 | ||
if (stone.x + nx in 0..7 && | ||
stone.y + ny in 0..7 | ||
) { | ||
// 돌 이동 후 킹 이동 | ||
stone.x += nx | ||
stone.y += ny | ||
king.x += nx | ||
king.y += ny | ||
} | ||
// 킹이 이동할 위치에 돌이 없다면 바로 이동 | ||
} else { | ||
king.x += nx | ||
king.y += ny | ||
} | ||
|
||
} | ||
println(positionToCoordinate(king)) | ||
println(positionToCoordinate(stone)) | ||
} | ||
|
||
fun coordinateToPosition(coordinate: String): Position { | ||
val (xPos, yPos) = coordinate.chunked(1) | ||
return Position( | ||
Alphabet.values().indexOf(Alphabet.valueOf(xPos)), | ||
yPos.toInt() - 1 | ||
) | ||
} | ||
|
||
fun positionToCoordinate(character: GameCharacter): String { | ||
return "${(Alphabet.values()[character.x])}${(character.y) + 1}" | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전체적으로 정리하면
class `전현수_킹` {
data class Position(val x: Int, val y: Int)
data class GameCharacter(var x: Int = 0, var y: Int = 0) {
operator fun plus(pos: Position) = run {
GameCharacter(x + pos.x, y + pos.y)
}
fun contain(xRange: IntRange, yRange: IntRange) = x in xRange && y in yRange
constructor(pos: Position) : this(pos.x, pos.y)
override fun toString(): String {
return "${Alphabet.values()[x]}${(y) + 1}"
}
}
enum class Dir(val position: Position) {
R(Position(1, 0)),
L(Position(-1, 0)),
B(Position(0, -1)),
T(Position(0, 1)),
RT(Position(1, 1)),
LT(Position(-1, 1)),
RB(Position(1, -1)),
LB(Position(-1, -1))
}
enum class Alphabet {
A, B, C, D, E, F, G, H
}
fun solution() {
val (kingLocation, stoneLocation, moveCnt) = readln().split(" ")
var king = GameCharacter(coordinateToPosition(kingLocation))
var stone = GameCharacter(coordinateToPosition(stoneLocation))
repeat(moveCnt.toInt()) {
val command = readln()
val dir: Dir = Dir.valueOf(command)
val nKing = king + dir.position
// 킹이 이동할 수 없는 위치면 명령 무시
if (!nKing.contain(0..7, 0..7)) {
return@repeat
}
// 킹이 이동할 위치에 돌이 있다면
if (nKing == stone) {
val nStone = stone + dir.position
// 돌이 움직일 수 있는 위치라면
if (nStone.contain(0..7, 0..7)) {
// 돌 이동 후 킹 이동
stone = nStone
king = nKing
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king = nKing
}
}
println("$king\n$stone")
}
fun coordinateToPosition(coordinate: String): Position {
val (xPos, yPos) = coordinate.chunked(1)
return Position(
Alphabet.values().indexOf(Alphabet.valueOf(xPos)),
yPos.toInt() - 1
)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
훨씬 더 객체 지향적으로... 너무 이쁘고 깔끔하네요 코드
if (king.x + nx == stone.x && | ||
king.y + ny == stone.y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
같은 객체의 data class면 자동으로 항목 비교가 되게끔!
king == stone
Issue Number
Issue #12
💎 문제 해결
추가로 푼 문제
🔥 특이사항/질문
흑.... 이번주 너무 바빠서 도피를 풀지 못했습니다ㅜㅜㅜ
죄송합니다ㅜㅡㅜ